package com.rentplant.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rentplant.Pojo.item;
import com.rentplant.Pojo.itemanduser;
import com.rentplant.service.impl.itemServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.text.Style;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@RestController//让它知道这是controller
@RequestMapping("/item")
public class itemController {

    @Autowired
    private itemServiceImp itemserviceimp;


//    发布物品
    @RequestMapping("/submitItem")
    public boolean addItem(@RequestBody JSONObject req){
        try {
//            System.out.println(req);
            return itemserviceimp.addItem(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

//    查询openid的物品
    @RequestMapping("/selectItem")
    public List<item> selectItem(@RequestBody JSONObject req){
        try{
            String openid = req.getString("openid");
            return itemserviceimp.selectItem(openid);
        }catch (Exception e){
            throw new RuntimeException(e);
        }

    }

//    保存草稿
    @RequestMapping("/saveItem")
    public boolean saveItem(@RequestBody JSONObject req){
        try {
            return itemserviceimp.saveItem(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }

    }

//    更新物品信息
    @RequestMapping("/updateItem")
    public boolean updateItem(@RequestBody JSONObject req) {
        try {
//            System.out.println(req);
            return itemserviceimp.updateItem(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }


//    根据物品id查询
    @RequestMapping("/selectItemById")
    public List<item> selectItemById(@RequestBody JSONObject req){
        try {
//            System.out.println(req);
            String itemid = req.getString("itemid");
            return itemserviceimp.selectItemById(itemid);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

//    展示主页信息
    @RequestMapping("/showIndexItem")
    public List<itemanduser> showIndexItem(){
        try {
            return itemserviceimp.showIndexItem();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

//    展示物品详情
    @RequestMapping("/selectItemByItemId")
    public List<itemanduser> selectItemByItemId(@RequestBody JSONObject req){
        try {
            String itemid = req.getString("itemid");
            return itemserviceimp.selectItemByItemId(itemid);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }


//    浏览量加一
    @RequestMapping("/updateSeeNum")
    public boolean  updateSeeNum(@RequestBody JSONObject req){
        try {
            String itemid = req.getString("itemid");
            return itemserviceimp.updateSeeNum(itemid);
        }catch (Exception e){
            throw  new RuntimeException(e);
        }
    }

//    分类查找物品
    @RequestMapping("/showTypeItem")
    public List<itemanduser> showTypeItem(@RequestBody JSONObject req){
        try {
            String itemtype = req.getString("itemtype");
            return itemserviceimp.showTypeItem(itemtype);
        }catch (Exception e){
            throw  new RuntimeException(e);
        }
    }

    //    展示物品类型 的综合排序
    @RequestMapping("/showAllItem")
    public List<itemanduser> showAllItem(@RequestBody JSONObject req){
        try {
            String itemtype = req.getString("itemtype");
            return itemserviceimp.showAllItem(itemtype);
        }catch (Exception e){
            throw  new RuntimeException(e);
        }
    }

    //    按照条件查询
    @RequestMapping("/selectItemByNew")
    public List<itemanduser> selectItemByNew(@RequestBody JSONObject req){
        try {
//            System.out.println(req);
            return itemserviceimp.selectItemByNew(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    按照条件查找销量高物品
    @RequestMapping("/selectItemByHot")
    public List<itemanduser> selectItemByHot(@RequestBody JSONObject req){
        try {
//            System.out.println(req);
            return itemserviceimp.selectItemByHot(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    物品按价格降序
    @RequestMapping("/selectItemByUp")
    public List<itemanduser> selectItemByUp(@RequestBody JSONObject req){
        try{
            return itemserviceimp.selectItemByUp(req);
        }catch( Exception e){
            throw  new RuntimeException(e);
        }

    }

    //物品按照价格升序
    @RequestMapping("/selectItemByDown")
    public List<itemanduser> selectItemByDown(@RequestBody JSONObject req){
        try {
            return itemserviceimp.selectItemByDown(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }





    //    按照itemid查找物品信息以及发布者信息
    @RequestMapping("/itemAndUser")
    public List<itemanduser> itemAndUser(@RequestBody JSONObject req){
        try {
            return itemserviceimp.itemAndUser(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //查询用户发布的物品
    @RequestMapping("/showUserItem")
    public List<item>showUserItem(@RequestBody JSONObject req){
        try {
            return itemserviceimp.showUserItem(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }

    }

    //查询用户未发布物品信息
    @RequestMapping("/showNoSubItem")
    public  List<item> showNoSubItem(@RequestBody JSONObject req){
        try {
            return itemserviceimp.showNoSubItem(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    查询用户已删除的物品
    @RequestMapping("/haveDelItem")
    public List<item>haveDelItem(@RequestBody JSONObject req){
        try {
            return itemserviceimp.haveDelItem(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    重新上架物品
    @RequestMapping("/renewItem")
    public boolean renewItem(@RequestBody JSONObject req){
        try {
            return itemserviceimp.renewItem(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //删除已发布物品
    @RequestMapping("/delHaveItem")
    public boolean delHaveItem (@RequestBody JSONObject req){
        try {
            return itemserviceimp.delHaveItem(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //发布草稿物品
    @RequestMapping("/addNoItem")
    public boolean addNoItem(@RequestBody JSONObject req){
        try {
            return itemserviceimp.addNoItem(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //删除用户草稿
    @RequestMapping("/delItem")
    public boolean delItem(@RequestBody JSONObject req){
        try {
            return itemserviceimp.delItem(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    租用物品后物品改为租用中
    @RequestMapping("/rentstate")
    public boolean rentitem(@RequestBody JSONObject req){
        try {
            return itemserviceimp.rentitem(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    增加出租次数
    @RequestMapping("/addrenttimes")
    public boolean addrenttimes(@RequestBody JSONObject req){
        try {
            return itemserviceimp.addrenttimes(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }




    //查找用户的搜索结果
    @RequestMapping("/showSelectResult")
    public List<itemanduser> showSelectResult(@RequestBody JSONObject req){
        try {
            String searchtext = req.getString("searchinfo");
            List<itemanduser> array = itemserviceimp.showSelectResult(searchtext);
            return array;

        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //用户查找按价格最高
    @RequestMapping("/showSelectByTop")
    public List<itemanduser> showSelectByTop(@RequestBody JSONObject req){
        try {
            String searchtext = req.getString("searchinfo");
            List<itemanduser> array = itemserviceimp.showSelectByTop(searchtext);
            return array;

        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //用户查找按价格最低
    @RequestMapping("/showSelectByLow")
    public List<itemanduser> showSelectByLow(@RequestBody JSONObject req){
        try {
            String searchtext = req.getString("searchinfo");
            List<itemanduser> array = itemserviceimp.showSelectByLow(searchtext);
            return array;

        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //用户查找销量最高
    @RequestMapping("/showSelectBySale")
    public List<itemanduser> showSelectBySale(@RequestBody JSONObject req){
        try {
            String searchtext = req.getString("searchinfo");
            List<itemanduser> array = itemserviceimp.showSelectBySale(searchtext);
            return array;

        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //用户查找最新
    @RequestMapping("/showSelectByNew")
    public List<itemanduser> showSelectByNew(@RequestBody JSONObject req){
        try {
            String searchtext = req.getString("searchinfo");
            List<itemanduser> array = itemserviceimp.showSelectByNew(searchtext);
            return array;

        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }


    //用户查找最热
    @RequestMapping("/showSelectByHot")
    public List<itemanduser> showSelectByHot(@RequestBody JSONObject req){
        try {
            String searchtext = req.getString("searchinfo");
            List<itemanduser> array = itemserviceimp.showSelectByHot(searchtext);
            return array;

        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }



    //    物品管理页面
//    渲染全部商品
    @RequestMapping("/allItem")
    public List<item> allItem(@RequestBody JSONObject req){
        try {
            return itemserviceimp.allItem(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //查询全部物品
    @RequestMapping("/allItems")
    public List<item> allItems(){
        try {
            return itemserviceimp.allItems();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    修改物品信息
    @RequestMapping("/updateItemInfo")
    public boolean updateItemInfo(@RequestBody JSONObject req){
        try {
            return itemserviceimp.updateItemInfo(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    按条件查找
    @RequestMapping("/searchItem")
    public List<item> searchItem(@RequestBody JSONObject req){
        try {
            return itemserviceimp.searchItem(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    按条件查找
    @RequestMapping("/searchItems")
    public List<item> searchItems(@RequestBody JSONObject req){
        try {
            return itemserviceimp.searchItems(req);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }


}
